// 设置公共路径
export const baseUrl = 'https://web.farmhelper.top'; 

function randomString() {
	return Math.random().toString(36).substring(7);
}

// 获取设备品牌、型号、设备 id 、系统名称、osVersion
const {
	deviceBrand,
	deviceModel,
	deviceId,
	osName,
	osVersion,
	appVersion
} = uni.getSystemInfoSync();

let operationSys = '';
let navigatorInfo = '';
let udid = '';
// #ifdef APP-PLUS
operationSys = "ios"
navigatorInfo = "ios"
// #endif
function asyncFunc() {
	// #ifdef APP-PLUS
	return new Promise((resolve, reject) => {
		plus.device.getInfo({
			success: e => {
				resolve(e.uuid);
			}
		})
	});
	// #endif
}
// #ifdef H5
operationSys = osName;
navigatorInfo = navigator.userAgent
// #endif

let deviceinfo = {
	"bag": "com.txjr.xhh",
	"operationSys": operationSys,
	"appVersion": appVersion,
	"channel": 'appStore',
	"brand": deviceBrand,
	"deviceModel": deviceModel,
	"osVersion": osVersion,
	"udid": udid,
	"isMobile": operationSys,
	"navigatorInfo": navigatorInfo,
	"deviceinfo": ''
};
var headers = {};

async function request(url, method, data, hideLoading) {
	udid = await asyncFunc();
	deviceinfo.udid = udid
	return new Promise((resolve, reject) => {
		const userToken = uni.getStorageSync('userToken');
		let token = null;
		if (userToken) {
			token = userToken;
		}
		// headers['content-type'] = 'application/x-www-form-urlencoded';
		headers['token'] = token;
		// headers['deviceinfo'] = JSON.stringify(deviceinfo);
		// 如果调用接口不明确不显示 loading
		if (!hideLoading) {
			uni.showLoading({
				title: '加载中'
			});
		}
		if (data && data.sign) {
			data.sign = null
		}
		// data = appendSign(data)
		uni.request({
			url: baseUrl + url,
			data: data,
			header: headers,
			method: method,
			success: (response) => {
				if (!hideLoading) {
					uni.hideLoading();
				}

				const result = response.data;
				if (result.code > 0) {
					resolve(result)
				} else if (result.code == '-1') {
					// token 过时 清除缓存
					uni.removeStorageSync("userInfo")
          uni.removeStorageSync("userToken")
					// uni.clearStorageSync();
					uni.redirectTo({
						url: '/pages/login/index'
					});
					return reject(result);
				} else {
					uni.showToast({
						title: result.msg || '请求失败',
						icon: 'none',
						duration: 2000,
					});
					reject(result)
				}

			},
			complete: () => {
				if (!hideLoading) {
					uni.hideLoading();
				}
				
			},
			fail: () => {
				if (!hideLoading) {
					uni.hideLoading();
				}
			}
		})
	})

}
const showError = error => {
	let errorMsg = '';

	switch (error.code) {
		case 10001:
			errorMsg = error.msg
			break
		case 401:
			errorMsg = '未授权，请登录'
			break
		case 403:
			errorMsg = '跨域拒绝访问'
			break
		case 404:
			errorMsg = '请求地址出错'
			break
		case 408:
			errorMsg = '请求超时'
			break
		case 500:
			errorMsg = '服务器内部错误'
			break
		case 501:
			errorMsg = '服务未实现'
			break
		case 502:
			errorMsg = '网关错误'
			break
		case 503:
			errorMsg = '服务不可用'
			break
		case 504:
			errorMsg = '网关超时'
			break
		case 505:
			errorMsg = 'HTTP版本不受支持'
			break
		default:
			errorMsg = error.msg
			break
	}

	uni.showToast({
		title: errorMsg,
		icon: 'none',
		duration: 2000,
		complete: function() {
			setTimeout(function() {
				uni.hideToast();
			}, 1000);
		}
	});
}
export default request;